<?php


namespace Home\Controller;

use Think\Controller;
// 指定允许其他域名访问
header('Access-Control-Allow-Origin:*');  
// 响应类型  
header('Access-Control-Allow-Methods:*');  
// 响应头设置  
header('Access-Control-Allow-Headers:x-requested-with,content-type');   

class DrivercarController extends Controller
{

    /**
     * @func 行车行程记录经纬度
     */
    public function locationdriver(){
        if(!IS_POST)
        {
            datamsg(0,'非法操作');
        }
        $paramdata=I("post.");
        $user=user_cinfo($paramdata['user_id'],$paramdata['token']);
        $car=M('car_basic')->where(array('openid'=>$user['openid']))->field("address,vehicleno")->find();
        $paramdata['address']=$car['address'];
        $paramdata['openid']=$user['openid'];
        $paramdata['vehicleno']=$car['vehicleno'];
        $paramdata['positiontime']=time();
        $data = M('locating_information')->add($paramdata);
        if($data){
            datamsg(1,'保存成功');
        }else{
            datamsg(0,'保存失败');
        }
    }


    /**
     * @func 驾驶员出车
     */
    public function getoutcar(){
        if(!IS_POST)
        {
            datamsg(0,'非法操作');
        }
        $user_id=I("user_id");
        $token=I("token");
        $user=user_cinfo($user_id,$token);

        //判断今天是否已经发车
        $currentday = date('Y-m-d',time());
        $start_day = $currentday." 00:00:00";
        $end_day = $currentday." 23:59:59";
        $map['openid']=$user['openid'];
        $map['upcartime']=array("between",array(strtotime($start_day),strtotime($end_day)));
        $isexit = M('getoutcar')->where($map)->find(); //查看今天是否已经出车了
        $data['openid']=$user['openid'];
        if($isexit){
            if($isexit['status'] == 1){
                //如果行程存在就修改
                $data['longituden']=I("longitude");
                $data['latituden']=I("latitude");
                $data['downcartime']=time();
                $data['status']=0;
                M('getoutcar')->where(array('openid'=>$user['openid']))->save($data);

                $data1   =   array(
                    'openid'         => $user['openid'],
                    'create_time'    => time(),
                    'status'         => 0,
                );
                D('cheliang_info')->add($data1); 
                datamsg(1,'下线成功');
            }else{
                $data1   =   array(
                    'openid'         => $user['openid'],
                    'create_time'    => time(),
                    'status'         => 1,
                );
                D('cheliang_info')->add($data1);

                //如果行程存在就修改
                $data['status']=1;
                M('getoutcar')->where(array('openid'=>$user['openid']))->save($data);
                datamsg(1,'上线成功');
            }
        }else{
            //如果行车不存在就添加记录
            $data['longitudeu']=I("longitude");
            $data['latitudeu']=I("latitude");
            $data['upcartime']=time();
            $data['status']=1;
            M('getoutcar')->add($data);
            datamsg(1,'上线成功');
        }




    }

}
